package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.Comment;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Comment entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Comment
 */
public class CommentDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(CommentDAO.class);
    // property constants
    public static final String COMMENT_DESCRIPTION_STAR = "commentDescriptionStar";
    public static final String COMMENT_SERVICE_STAR = "commentServiceStar";
    public static final String COMMENT_DELIVER_STAR = "commentDeliverStar";
    public static final String COMMENT_LOGISTICS_STAR = "commentLogisticsStar";
    public static final String COMMENT_COURIER_STAR = "commentCourierStar";
    public static final String COMMENT_CONTENT = "commentContent";
    public static final String COMMENT_GALLERY = "commentGallery";
    public static final String COMMENT_REPLY = "commentReply";
    public static final String COMMENT_REPLY_SELLER_ID = "commentReplySellerId";
    public static final String BUYER_ID = "buyerId";
    public static final String GOODS_ID = "goodsId";
    public static final String ORDER_ID = "orderId";

    public Comment save(Comment transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }


    public List<Comment> saveList(List<Comment> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (Comment temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }


    public int delete(Comment persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Comment findById(java.lang.String id)
    {
        log.debug("getting Comment instance with id: " + id);
        try
        {
            Comment instance = (Comment) getSession().get("com.qianhui.pojo.Comment", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Comment instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Comment as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByCommentDescriptionStar(Object commentDescriptionStar)
    {
        return findByProperty(COMMENT_DESCRIPTION_STAR, commentDescriptionStar);
    }

    public List findByCommentServiceStar(Object commentServiceStar)
    {
        return findByProperty(COMMENT_SERVICE_STAR, commentServiceStar);
    }

    public List findByCommentDeliverStar(Object commentDeliverStar)
    {
        return findByProperty(COMMENT_DELIVER_STAR, commentDeliverStar);
    }

    public List findByCommentLogisticsStar(Object commentLogisticsStar)
    {
        return findByProperty(COMMENT_LOGISTICS_STAR, commentLogisticsStar);
    }

    public List findByCommentCourierStar(Object commentCourierStar)
    {
        return findByProperty(COMMENT_COURIER_STAR, commentCourierStar);
    }

    public List findByCommentContent(Object commentContent)
    {
        return findByProperty(COMMENT_CONTENT, commentContent);
    }

    public List findByCommentGallery(Object commentGallery)
    {
        return findByProperty(COMMENT_GALLERY, commentGallery);
    }

    public List findByCommentReply(Object commentReply)
    {
        return findByProperty(COMMENT_REPLY, commentReply);
    }

    public List findByCommentReplySellerId(Object commentReplySellerId)
    {
        return findByProperty(COMMENT_REPLY_SELLER_ID, commentReplySellerId);
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByGoodsId(Object goodsId)
    {
        return findByProperty(GOODS_ID, goodsId);
    }

    public List findByOrderId(Object orderId)
    {
        return findByProperty(ORDER_ID, orderId);
    }

    public List findAll()
    {
        log.debug("finding all Comment instances");
        try
        {
            String queryString = "from Comment";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


    public List getListByBuyerIdAndIndexAndPageSize(String buyerId, int index, int pageSize)
    {
        try
        {
            String sql = "from Comment c where c.buyerId=:buyerId order by c.commentContentTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerId(String buyerId)
    {
        try
        {
            String sql = "select count(*) from Comment c where c.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


    public List getListByGoodsIdAndIndexAndPageSize(String goodsId, int index, int pageSize)
    {
        try
        {
            String sql = "from Comment c where c.goodsId=:goodsId order by c.commentContentTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("goodsId", goodsId);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByGoodsId(String goodsId)
    {
        try
        {
            String sql = "select count(*) from Comment c where c.goodsId=:goodsId";
            Query query = getSession().createQuery(sql);
            query.setParameter("goodsId", goodsId);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}